//
//  YDAccountManager.m
//  IngCard
//
//  Created by kf on 2017/7/10.
//  Copyright © 2017年 IngDan. All rights reserved.
//

#import "YDAccountManager.h"
#import "PPNetworkHelper.h"
#import "YDHttpClient.h"
#import "YDContactsManager.h"

@interface YDAccountManager ()
@property(nonatomic, strong) YDAccount *account;
@end

@implementation YDAccountManager
+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    static YDAccountManager *instance = nil;
    dispatch_once(&onceToken,^{
        instance = [[super allocWithZone:NULL] init];
        instance.Notification_userLogin = @"NOTIFICATION_USER_LOGIN";
        instance.Notification_userLoginOut = @"NOTIFICATION_USER_LOGINOUT";
        [instance.account loadDB];
    });
    return instance;
}

+ (id)allocWithZone:(struct _NSZone *)zone{
    return [self sharedInstance];
}

-(void) getVerifyCode:(NSString *)phoneNumber
             isLogion:(BOOL) isLogin
           onComplete:(void (^)(BOOL success, NSString* msg)) onComplete
{
    [HTTP POST:API.common_getVerifyCode
    parameters:^(NSMutableDictionary *parameters) {
        [parameters safeSetObject:phoneNumber forKey:@"mobile"];
        [parameters safeSetObject:(isLogin ? @"login" : @"register") forKey:@"cate"];
    }
    onComplete:^(BOOL success, NSDictionary *response, NSString *msg) {
        BlockGoGo(onComplete, success, msg);
    }];
}


-(void) registerWithPhoneNumber:(NSString *)phoneNumber
                     verifyCode:(NSString *)verifyCode
                     onComplete:(void (^)(BOOL success, NSString* msg)) onComplete;
{
    [HTTP POST:API.account_regist
    parameters:^(NSMutableDictionary *parameters) {
        [parameters safeSetObject:phoneNumber forKey:@"mobile"];
        [parameters safeSetObject:verifyCode forKey:@"password"];
    } onComplete:^(BOOL success, NSDictionary *response, NSString *msg) {
        
        if (success)
        {
            self.account.uid = NSStringMake(response[@"uid"]);
            self.account.mobile = NSStringMake(response[@"mobile"]);
            self.account.login_token = NSStringMake(response[@"login_token"]);
        }
        
        BlockGoGo(onComplete,success,msg);
    }];
}

-(void) loginWithPhoneNumber:(NSString *)phoneNumber
                 verifyCodeL:(NSString *)verifyCode
                  onComplete:(void (^)(BOOL success, NSString* msg)) onComplete
{
    [HTTP POST:API.account_login parameters:^(NSMutableDictionary *parameters) {
        [parameters safeSetObject:phoneNumber forKey:@"mobile"];
        [parameters safeSetObject:verifyCode forKey:@"password"];
    } onComplete:^(BOOL success, NSDictionary *response, NSString *msg) {
        
        if (success)
        {
            self.account.uid = NSStringMake(response[@"uid"]);
            self.account.login_token = NSStringMake(response[@"login_token"]);
        }
        
        BlockGoGo(onComplete, success, msg);
        
        [self getAccountDetail:nil];
        
        if (success)
        {
            NSNotificationMake(self.Notification_userLogin, nil);
        }
    }];
}


- (void) getAccountDetail:(void (^)(BOOL success, NSString* msg)) onComplete
{
    [HTTP POST:API.account_detail parameters:^(NSMutableDictionary *parameters) {
        [parameters safeSetObject:self.account.uid forKey:@"uid"];
        [parameters safeSetObject:self.account.login_token forKey:@"login_token"];
    } onComplete:^(BOOL success, NSDictionary *response, NSString *msg) {
        if(success)
        {
            [self.account serializeWith:response];
            
            [CONTACT getListOnComplete:^(BOOL success, NSString *msg) {
                
            }];
        }
        BlockGoGo(onComplete,success,msg);
    }];
}

-(void) loginoutOnComplete:(void (^)(BOOL success, NSString* msg)) onComplete;
{
    [HTTP POST:API.account_loginout parameters:^(NSMutableDictionary *parameters) {
        [parameters safeSetObject:self.account.uid forKey:@"uid"];
        [parameters safeSetObject:self.account.login_token forKey:@"login_token"];
    } onComplete:^(BOOL success, NSDictionary *response, NSString *msg) {
        [self.account clear];
        self.account = nil;
        
        BlockGoGo(onComplete, success, msg);
        NSNotificationMake(self.Notification_userLoginOut, nil);
    }];
}


-(void) updateBaseInfo:(NSString *) firstName
              lastName:(NSString *) lastName
               company:(NSString *) company
            occupation:(NSString *) occupation
                 email:(NSString *) email
              location:(NSString *) location
            onComplete:(void (^)(BOOL success, NSString* msg)) onComplete
{
    [HTTP POST:API.account_updateBaseInfo parameters:^(NSMutableDictionary *parameters) {
        
        [parameters safeSetObject:self.account.uid forKey:@"uid"];
        [parameters safeSetObject:self.account.login_token forKey:@"login_token"];
        [parameters safeSetObject:firstName forKey:@"first_name"];
        [parameters safeSetObject:lastName forKey:@"last_name"];
        [parameters safeSetObject:company forKey:@"company_name"];
        [parameters safeSetObject:email forKey:@"email"];
        
        [parameters safeSetObject:location forKey:@"location"];
        [parameters safeSetObject:occupation forKey:@"occupation"];
        
    } onComplete:^(BOOL success, NSDictionary *response, NSString *msg) {
        BlockGoGo(onComplete, success, msg);
        
        [self getAccountDetail:nil];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSNotificationMake(self.Notification_userLogin, nil);
        });
    }];
}



-(YDAccount *) account
{
    if (!_account)
    {
        _account = [YDAccount new];
    }
    return _account;
}


@end

